import hilog from '@ohos.hilog'
import { FileItem } from '../../file_manager/FileItem'
import { LogLevel, LogTarget } from './LogBasedata'
import { LogEvent } from './LogEvent'
import { Logger } from './Logger'
import { LogTargetDailyRollingFile } from './log_target/LogTargetDailyRollingFile'
import { Context } from '@ohos.abilityAccessCtrl'

/**
 * 不同等级的日志,支持配置到不同的文件夹.
 * LogTarget不放在LogManager里面?
 *
 */
export class LogManager {
  static TAG1 = "__FirstPage"
  static TAG2 = "__Refresh"
  static TAG3 = "__Refresh"
  static TAGC = "__Current"

  logger: Logger;

  //debug,console.
  static debug_c(context: string, content: string) {
    LogManager.sharedInstance().log(LogLevel.Debug, LogTarget.Console, context, content);
  }

  static info_c(context: string, content: string) {
    LogManager.sharedInstance().log(LogLevel.Debug, LogTarget.Console, context, content);
  }

  static warn_c(context: string, content: string) {
    LogManager.sharedInstance().log(LogLevel.Debug, LogTarget.Console, context, content);
  }

  static error_c(context: string, content: string) {
    LogManager.sharedInstance().log(LogLevel.Debug, LogTarget.Console, context, content);
  }

  static fatal_c(context: string, content: string) {
    LogManager.sharedInstance().log(LogLevel.Debug, LogTarget.Console, context, content);
  }

  static debug(context: string, content: string) {
    LogManager.sharedInstance().log(LogLevel.Debug, LogTarget.All, context, content);
  }

  static info(context: string, content: string) {
    LogManager.sharedInstance().log(LogLevel.Debug, LogTarget.All, context, content);
  }

  static warn(context: string, content: string) {
    LogManager.sharedInstance().log(LogLevel.Debug, LogTarget.All, context, content);
  }

  static error(context: string, content: string) {
    LogManager.sharedInstance().log(LogLevel.Debug, LogTarget.All, context, content);
  }

  static fatal(context: string, content: string) {
    LogManager.sharedInstance().log(LogLevel.Debug, LogTarget.All, context, content);
  }

  public static context: Context | null = null;

  /**
   * 创建单例使用的folder.
   * @param context
   * @param defaultDirFolder
   */
  static initClass(context: Context, defaultDirFolder?: string) {
    LogManager.context = context;
    LogManager.defaultDirFolder = defaultDirFolder;
  }

  //true 只输出用户输入的文本,不输出时间等信息.
  public isPlain: boolean = false;
  private static _sharedInstance: LogManager | null = null;
  private static defaultDirFolder: string | null = null;

  public static sharedInstance(): LogManager {
    if (!LogManager._sharedInstance) {
      LogManager._sharedInstance = new LogManager(LogManager.defaultDirFolder!);
    }
    return LogManager._sharedInstance;
  }

  constructor(logDirPath: string, fileInitailContent: string = "") {
    this.logger = new Logger(logDirPath, fileInitailContent);
  }

  log(level: LogLevel, target: LogTarget, context: string, content: string) {
    let event = new LogEvent(level, target, context, content);
    this.logger.log(event);
  }

  getLogs(): string[] {
    return this.logger.getLogs();
  }

  private getFileTarget(): LogTargetDailyRollingFile {
    return this.logger.fileTarget;
  }

  clearLogs() {
    this.getFileTarget().clearLogs();
  }
}

//
// //等级值越小越严重,大于该值的,不输出.
// - (void)setLogLevelFilter:(int)filter {
//   self.logger.logLevel = filter;
// }
// //设置了标签后,只有标签中的日志才会输出,其余不输出.
// - (void)setLogTags:(NSArray *)logTags {
//   self.logger.logTags = logTags;
// }
//
// //设置日志输出目录
// - (void)setLogDirPath:(NSString *)logDirPath {
//   self.logger.fileTarget.logDirectoryPath = logDirPath;
// }
// //获取日志目录路径
// - (NSString *)getLogDirPath {
//   return self.logger.fileTarget.logDirectoryPath;
// }
// - (void)printLogFileDir {
// 	NSString * logDirPath = [self getLogDirPath];
// 	jslogv_fc(@"JsLogManager",nil, @"Log dir is:%@",logDirPath);
// }
//
// //获取今日的日志文件路径
// - (NSString *)getTodayLogFilePath {
//   return self.logger.fileTarget.currentFilePath;
// }
//
// //设置最大的日志文件数量
// - (void)setMaxLogFileCount:(int)count {
//   self.logger.fileTarget.maxFileCount = count;
// }
//

// - (void)clearTodayLog {
// 	NSString * filePath = [self getTodayLogFilePath];
// 	[JsFileManager emptyFile:filePath];
// }
// }